package com.h117121.fashionshop.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.h117121.fashionshop.controller.generic.GenericController;
import com.h117121.fashionshop.core.dom.SettingForm;
import com.h117121.fashionshop.validator.SettingValidator;

@Controller
@RequestMapping("/admin/setting")
public class AdminSettingController extends GenericController {
	@Autowired
	private SettingValidator validator;

	@RequestMapping(method = RequestMethod.GET)
	protected String showPage(ModelMap model) {
		model.addAttribute("setting", new SettingForm());
		return "AdminSettingPage";
	}

	@RequestMapping(method = RequestMethod.POST)
	protected String saveSetting(
			@ModelAttribute("setting") SettingForm setting,
			BindingResult errors, ModelMap model) {
		validator.validate(setting, errors);
		if (!errors.hasErrors()) {
			setting.saveSetting();
			model.addAttribute("successfulMessage", true);
		}
		return "AdminSettingPage";
	}

}